package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanYuanKuPeiXunSavePO;
import com.ct.application.shipping.model.po.ChuanYuanZhengShuSavePO;
import com.ct.application.shipping.model.po.ChuanYuanZhengShuSearchPO;
import com.ct.application.shipping.model.vo.ChuanYuanKuPeiXunVO;
import com.ct.application.shipping.model.vo.ChuanYuanZhengShuVO;
import com.ct.application.shipping.service.ChuanYuanZhengShuService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanYuanZhengShuMapper;
import com.ct.shipping.orm.ChuanBoXinXiDO;
import com.ct.shipping.orm.ChuanYuanKuPeiXunDO;
import com.ct.shipping.orm.ChuanYuanZhengShuDO;
import com.ct.utils.result.StatusEnums;
import com.ct.utils.threadlocal.UserThreadLocal;
import com.ct.utils.valid.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 船员证书 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanYuanZhengShuServiceImpl extends BaseServiceImpl<ChuanYuanZhengShuMapper, ChuanYuanZhengShuDO>
        implements ChuanYuanZhengShuService {


    @Override
    public List<ChuanYuanZhengShuVO> findChuanYuanZhengShusBiao(){
        LambdaQueryWrapper<ChuanYuanZhengShuDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChuanYuanZhengShuDO::getZhuangTai, StatusEnums.STATUS.getCode());
        return tranferListBo(this.list(lambdaQueryWrapper),ChuanYuanZhengShuVO.class);
    }

    /**
     * 查询分页列表
     */
    @Override
    public Page<ChuanYuanZhengShuVO> findChuanYuanZhengShu(ChuanYuanZhengShuSearchPO searchPO) {
        LambdaQueryWrapper<ChuanYuanZhengShuDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(searchPO.getKeyword())){
            lambdaQueryWrapper.and(w->{
                w.like(
                        ChuanYuanZhengShuDO::getXiangMuMing,searchPO.getKeyword())
                        .or()
                        .like(ChuanYuanZhengShuDO::getXiangMuDaiMa,searchPO.getKeyword());
            });
        }
        lambdaQueryWrapper.orderByDesc(ChuanYuanZhengShuDO::getCreateTime);
        Page<ChuanYuanZhengShuDO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        Page<ChuanYuanZhengShuDO> page1 = this.page(page, lambdaQueryWrapper);
        return tranferPageBo(page1, ChuanYuanZhengShuVO.class);
    }


    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveChuanYuanZhengShu(ChuanYuanZhengShuSavePO savePO){
        savePO.setId(null); //新增不需要id
        ChuanYuanZhengShuDO chuanYuanZhengShuDO = tranferBo(savePO, ChuanYuanZhengShuDO.class);
        if (Objects.nonNull(chuanYuanZhengShuDO)){
            chuanYuanZhengShuDO.setSystemId(UserThreadLocal.get().getSystemId());
            chuanYuanZhengShuDO.setSystemName(UserThreadLocal.get().getSystemName());
            return this.save(chuanYuanZhengShuDO);
        }
        if(!savePO.getYouXiaoQi().matches("^\\d+$")){
            throw new RuntimeException("有效期只能是正整数数字");
        }
        if(!savePO.getYuJingTianShu().matches("^\\d+$")){
            throw new RuntimeException("预警天数只能是正整数数字");
        }
        return false;
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateChuanYuanZhengShu(ChuanYuanZhengShuSavePO savePO){
        if (savePO.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        if(!savePO.getYouXiaoQi().matches("^\\d+$")){
            throw new RuntimeException("有效期只能是正整数数字");
        }
        if(!savePO.getYuJingTianShu().matches("^\\d+$")){
            throw new RuntimeException("预警天数只能是正整数数字");
        }
        ChuanYuanZhengShuDO chuanYuanZhengShuDO = tranferBo(savePO, ChuanYuanZhengShuDO.class);
        if (Objects.nonNull(chuanYuanZhengShuDO)){
            return this.updateById(chuanYuanZhengShuDO);
        }
        return false;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delChuanYuanZhengShu(Long id){
        return this.removeById(id);
    }


    /**
     * 查看证书详情
     */
    @Override
    public ChuanYuanZhengShuVO getChuanYuanZhengShu(Long id){
        ChuanYuanZhengShuDO chuanYuanZhengShuDO = getById(id);
        return tranferBo(chuanYuanZhengShuDO,ChuanYuanZhengShuVO.class);
    }

    /**
     * 职位状态修改。
     * 0:停用、1:启用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return this.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return this.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }
}
